package com.oa.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.oa.core.config.WechatConfig;
import com.oa.core.constant.ServerInterface;
import com.oa.core.model.Message;
import com.oa.core.model.SysMsg;
import com.oa.core.model.WeChatText;
import com.oa.core.model.WeChatTextMsg;
import com.oa.system.entity.User;
import com.oa.system.service.IUserService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.function.Consumer;

@Component
public class SendMsgManager {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IUserService userService;

    public void sendMsg(User user, String title, String remark){
        SysMsg sysMsg = new SysMsg();
        sysMsg.setConnectId(user.getUsername() + "_" + user.getPassword());
        Message message = new Message();
        message.setTitle(title);
        message.setContent(remark);
        sysMsg.setMessage(message);
        restTemplate.postForObject(ServerInterface.WEB_SOCKET + "/msg/sys/send/"+sysMsg.getConnectId(), sysMsg, String.class);
    }

    public void sendMsg(User user, String title, String remark, Consumer<Res> consumer){
        SysMsg sysMsg = new SysMsg();
        sysMsg.setConnectId(user.getUsername() + "_" + user.getPassword());
        Message message = new Message();
        message.setTitle(title);
        message.setContent(remark);
        sysMsg.setMessage(message);

        restTemplate.postForObject(ServerInterface.WEB_SOCKET + "/msg/sys/send/"+sysMsg.getConnectId(), sysMsg, String.class);
        Res res = new Res();
        consumer.accept(res);
    }

    public void sendMsgAll(String title, String remark){
        SysMsg sysMsg = new SysMsg();
        sysMsg.setConnectId("0");
        Message message = new Message();
        message.setTitle(title);
        message.setContent(remark);
        sysMsg.setMessage(message);
        restTemplate.postForObject(ServerInterface.WEB_SOCKET + "/msg/sys/send/all", sysMsg, String.class);
    }

    public void sendWeChatMsg(User user, String content){
        WeChatText weChatText = new WeChatText();
        weChatText.setContent(content);

        WeChatTextMsg weChatTextMsg = new WeChatTextMsg();
        weChatTextMsg.setAgentid(WechatConfig.publicNumberAgentid);
        weChatTextMsg.setSafe(0);
        weChatTextMsg.setMsgtype("text");
        weChatTextMsg.setTouser(user.getWechatId());

        restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+CompanyWechatUtil.getToken(), weChatTextMsg, String.class);
    }

    @ApiModel(description = "响应体")
    @Data
    public class Res<T>{
        @ApiModelProperty("代码")
        private String code;

        @ApiModelProperty("消息")
        private String msg;

        @ApiModelProperty("数据")
        private T data;
    }
}
